﻿#include "bundledesignerdialog.h"
#include "ui_bundledesignerdialog.h"
#include <QUuid>
#include <QTextStream>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QFileDialog>
#include <QInputDialog>
#include <QMessageBox>

#include "actiondialog.h"
#include "widgetdialog.h"


BundleDesignerDialog::BundleDesignerDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::BundleDesignerDialog)
{
    ui->setupUi(this);

    this->setWindowTitle(tr("Bundle Designer"));
    this->setObjectName("WidgetBundleDesignerObject");

    ui->treeWidget_Action->header()->setDefaultAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    ui->treeWidget_Widget->header()->setDefaultAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    ui->treeWidget_Action->header()->setSectionResizeMode(QHeaderView::Stretch);
    ui->treeWidget_Widget->header()->setSectionResizeMode(QHeaderView::Stretch);
    ui->lineEdit_Name->setText(QString("Plugin%1Test").arg(QUuid::createUuid().toString().section("-",1,1)));
}

BundleDesignerDialog::~BundleDesignerDialog()
{
    delete ui;
}


void BundleDesignerDialog::on_pushButton_GenerateFiles_clicked()
{
    QDir dir(QDir::currentPath());
    dir.cdUp();
//    dir.cdUp();

//    QString sPath = QFileDialog::getExistingDirectory(this,tr("Select Generate Path"),dir.absolutePath());
    QString sPath = dir.absolutePath();

    if(sPath.isEmpty())
        return;

    QString sPluginName = ui->lineEdit_Name->text();

    this->generateCode(sPath,sPluginName);

    QMessageBox::information(this,tr("Infomation"),QString("Your Bundle Code is Generated at:%1/%2").arg(sPath).arg(sPluginName));
}

void BundleDesignerDialog::generateCode(const QString &dir, const QString &pluginName)
{
    Q_UNUSED(pluginName)

    QDir fileDir(dir);
    fileDir.mkdir(ui->lineEdit_Name->text());
    fileDir.cd(ui->lineEdit_Name->text());

    QFile file(fileDir.absolutePath()+"/MANIFEST.MF");
    if(!file.open(QIODevice::WriteOnly))
    {
        return;
    }

    QTextStream out(&file);
    out<<"Plugin-SymbolicName: "<<ui->lineEdit_Name->text()<<"\r\n";
    out<<"Plugin-ActivationPolicy: "<<"eager"<<"\r\n";
    out<<"Plugin-Category: "<<"complugin"<<"\r\n";
    out<<"Plugin-ContactAddress: "<<"15011502134@163.com"<<"\r\n";
    out<<"Plugin-Description: "<<"plugin developed with c++,with qt platform"<<"\r\n";
    out<<"Plugin-Name: "<<ui->lineEdit_Name->text()<<"\r\n";
    out<<"Plugin-Vendor: "<<"Copyright 2018-2023 The KT Team. All rights reserved."<<"\r\n";
    out<<"Plugin-Version: "<<ui->lineEdit_Version->text()<<"\r\n";

    file.close();

    generateProFile(fileDir.absolutePath());
    generateResource(fileDir.absolutePath());
    generateBundleHeader(fileDir.absolutePath());
    generateBundleCpp(fileDir.absolutePath());
    for(int i=0; i<ui->treeWidget_Widget->topLevelItemCount(); ++i)
    {
        generateWidgetFile(fileDir.absolutePath(),i);
    }


}

void BundleDesignerDialog::generateProFile(const QString &dir)
{
    QFile tempFile(":/ModelFile/BundleDesigner/protemp.temp");
    if(!tempFile.open(QIODevice::ReadOnly))
    {
        return;
    }

    QTextStream in(&tempFile);

    QDir fileDir(dir);
    QString sClassName = ui->lineEdit_Name->text();
    QFile proFile(fileDir.absolutePath()+"/"+sClassName+".pro");
    if(!proFile.open(QIODevice::WriteOnly))
    {
        return;
    }
    QTextStream out(&proFile);

    while(!in.atEnd())
    {
        QString sLine = in.readLine();
        if(sLine.contains("[widget_cpp]"))
        {
            for(int i=0; i<ui->treeWidget_Widget->topLevelItemCount(); ++i)
            {
                QString widgetClass = ui->treeWidget_Widget->topLevelItem(i)->data(0,Qt::WhatsThisRole).toString();
                QString sOut = sLine;
                sOut = sOut.replace("[widget_cpp]",widgetClass.toLower());
                out<<sOut<<"\r\n";
            }
            continue;
        }
        else if(sLine.contains("[widget_h]"))
        {
            for(int i=0; i<ui->treeWidget_Widget->topLevelItemCount(); ++i)
            {
                QString widgetClass = ui->treeWidget_Widget->topLevelItem(i)->data(0,Qt::WhatsThisRole).toString();
                QString sOut = sLine;
                sOut = sOut.replace("[widget_h]",widgetClass.toLower());
                out<<sOut<<"\r\n";
            }
            continue;
        }
        else if(sLine.contains("[widget_ui]"))
        {
            for(int i=0; i<ui->treeWidget_Widget->topLevelItemCount(); ++i)
            {
                QString widgetClass = ui->treeWidget_Widget->topLevelItem(i)->data(0,Qt::WhatsThisRole).toString();
                QString sOut = sLine;
                sOut = sOut.replace("[widget_ui]",widgetClass.toLower());
                bool bgenUIFile = ui->treeWidget_Widget->topLevelItem(i)->data(3,Qt::WhatsThisRole).toBool();

                if(bgenUIFile)
                {
                    out<<sOut<<"\r\n";
                }
            }
            continue;
        }
        else if(sLine.contains("[dependslib_header]"))
        {
#if 0
            int n = ui->treeWidget_DependLib->topLevelItemCount();
            for(int i=0; i<n; ++i)
            {
                QString sOut = sLine.section("[dependslib_header]",0,0);
                if(ui->treeWidget_DependLib->topLevelItem(i)->isSelected())
                {
                    QString s = ui->treeWidget_DependLib->topLevelItem(i)->data(1,Qt::WhatsThisRole).toString();
                    QStringList lstValue = s.split(";");
                    QString sIncPath,sDebugLibName,sReleaseLibName;
                    foreach (QString sValue, lstValue) {
                        if(sValue.contains("INC:"))
                        {
                            sIncPath = sValue.section("INC:",1,1);
                        }
                        else if(sValue.contains("LDBG:"))
                        {
                            sDebugLibName = sValue.section("LDBG:",1,1);
                        }
                        else if(sValue.contains("LRSE:"))
                        {
                            sReleaseLibName = sValue.section("LRSE:",1,1);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    QStringList lstHeader = sIncPath.split("+");
                    foreach (QString sHeader, lstHeader) {
                        out<<sOut<<sHeader<<"\r\n";
                    }
                }
            }
#endif
            continue;
        }
        else if(sLine.contains("[dependslib_debug]"))
        {
#if 0
            int n = ui->treeWidget_DependLib->topLevelItemCount();
            for(int i=0; i<n; ++i)
            {
                QString sOut = sLine.section("[dependslib_debug]",0,0);
                if(ui->treeWidget_DependLib->topLevelItem(i)->isSelected())
                {
                    QString s = ui->treeWidget_DependLib->topLevelItem(i)->data(1,Qt::WhatsThisRole).toString();
                    QStringList lstValue = s.split(";");
                    QString sIncPath,sDebugLibName,sReleaseLibName;
                    foreach (QString sValue, lstValue) {
                        if(sValue.contains("INC:"))
                        {
                            sIncPath = sValue.section("INC:",1,1);
                        }
                        else if(sValue.contains("LDBG:"))
                        {
                            sDebugLibName = sValue.section("LDBG:",1,1);
                        }
                        else if(sValue.contains("LRSE:"))
                        {
                            sReleaseLibName = sValue.section("LRSE:",1,1);
                        }
                        else
                        {
                            continue;
                        }
                    }

                    QStringList lstHeader = sDebugLibName.split("+");
                    foreach (QString sHeader, lstHeader) {
                        out<<sOut<<sHeader<<"\r\n";
                    }
                }
            }
#endif
            continue;
        }
        else if(sLine.contains("[dependslib_release]"))
        {
#if 0
            int n = ui->treeWidget_DependLib->topLevelItemCount();
            for(int i=0; i<n; ++i)
            {
                QString sOut = sLine.section("[dependslib_release]",0,0);
                if(ui->treeWidget_DependLib->topLevelItem(i)->isSelected())
                {
                    QString s = ui->treeWidget_DependLib->topLevelItem(i)->data(1,Qt::WhatsThisRole).toString();
                    QStringList lstValue = s.split(";");
                    QString sIncPath,sDebugLibName,sReleaseLibName;
                    foreach (QString sValue, lstValue) {
                        if(sValue.contains("INC:"))
                        {
                            sIncPath = sValue.section("INC:",1,1);
                        }
                        else if(sValue.contains("LDBG:"))
                        {
                            sDebugLibName = sValue.section("LDBG:",1,1);
                        }
                        else if(sValue.contains("LRSE:"))
                        {
                            sReleaseLibName = sValue.section("LRSE:",1,1);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    QStringList lstHeader = sReleaseLibName.split("+");
                    foreach (QString sHeader, lstHeader) {
                        out<<sOut<<sHeader<<"\r\n";
                    }
                }
            }
#endif
            continue;
        }
        else if(sLine.contains("[serviceinterfacebegin]"))
        {
#if 0
            sLine = in.readLine();
            QStringList sInterfaceList;
            sInterfaceList.clear();
            for(int i=0; i<ui->widget->topLevelItemCount(); ++i)
            {
                QTreeWidgetItem* pTopItem = ui->widget->topLevelItem(i);
                if(nullptr == pTopItem)continue;
                QString sInterface = pTopItem->data(0,Qt::DisplayRole).toString();
                if(sInterface.isEmpty())continue;
                sInterfaceList.append(sInterface);
            }

            while((!sLine.contains("[serviceinterfaceend]") )&& (!in.atEnd()))
            {
                if(sInterfaceList.count() > 0)
                {
                    QString sOut = sLine;
                    out<<sOut<<"\r\n";
                }

                sLine = in.readLine();
            }
#endif
        }
        else
        {
            QString sOut = sLine;
            sOut = sOut.replace("[name]",sClassName);
            sOut = sOut.replace("[name_lower]",sClassName.toLower());
            sOut = sOut.replace("[name_upper]",sClassName.toUpper());
            out<<sOut<<"\r\n";
        }

    }

    proFile.close();
    tempFile.close();
}

void BundleDesignerDialog::generateResource(const QString &dir)
{
    QFile tempFile(":/ModelFile/BundleDesigner/resource.temp");
    if(!tempFile.open(QIODevice::ReadOnly))
    {
        return;
    }

    QTextStream in(&tempFile);

    QDir fileDir(dir);
    QString sClassName =  ui->lineEdit_Name->text();
    QFile proFile(fileDir.absolutePath()+"/"+sClassName+".qrc");
    if(!proFile.open(QIODevice::WriteOnly))
    {
        return;
    }
    QTextStream out(&proFile);

    while(!in.atEnd())
    {
        QString sLine = in.readLine();
        QString sOut = sLine;
        sOut = sOut.replace("[name]",sClassName);
        sOut = sOut.replace("[name_lower]",sClassName.toLower());
        sOut = sOut.replace("[name_upper]",sClassName.toUpper());
        if(sLine.contains("[iconfilename]"))
        {
            // 每个菜单涉及的图标自动生成
            for(int i=0; i<ui->treeWidget_Action->topLevelItemCount(); ++i)
            {
                QString sIconPath = ui->treeWidget_Action->topLevelItem(i)->data(3,Qt::WhatsThisRole).toString();
                if(sIconPath.isEmpty())
                    continue;

                // 每次循环重新给赋值
                sOut = sLine;

                QString sIconName = sIconPath.section("/",-1,-1);
                QDir tarDir(dir);
                tarDir.mkdir("res");
                tarDir.cd("res");

                QFile file(sIconPath);
                file.copy(tarDir.absoluteFilePath(sIconName));
                sOut = sOut.replace("[iconfilename]",sIconName);
                out<<sOut<<"\r\n";
            }

            // 每个窗口涉及的图标自动生成
            for(int i=0; i<ui->treeWidget_Widget->topLevelItemCount(); ++i)
            {
                QString sIconPath = ui->treeWidget_Widget->topLevelItem(i)->data(2,Qt::DisplayRole).toString();
                if(sIconPath.isEmpty())
                    continue;

                QString sIconName = sIconPath.section("/",-1,-1);
                QDir tarDir(dir);
                tarDir.mkdir("res");
                tarDir.cd("res");

                QFile file(sIconPath);
                file.copy(tarDir.absoluteFilePath(sIconName));
                sOut = sOut.replace("[iconfilename]",sIconName);
                out<<sOut<<"\r\n";

            }
            continue;
        }

        out<<sOut<<"\r\n";
    }

    proFile.close();
    tempFile.close();
}

void BundleDesignerDialog::generateBundleHeader(const QString &dir)
{
    QFile tempFile(":/ModelFile/BundleDesigner/bundleheader.temp");
    if(!tempFile.open(QIODevice::ReadOnly))
    {
        return;
    }

    QTextStream in(&tempFile);

    QDir fileDir(dir);
    QString sClassName =  ui->lineEdit_Name->text();
    QFile proFile(fileDir.absolutePath()+"/"+sClassName.toLower()+".h");
    if(!proFile.open(QIODevice::WriteOnly))
    {
        return;
    }
    QTextStream out(&proFile);

    while(!in.atEnd())
    {
        QString sLine = in.readLine();

        if(sLine.contains("[widgetheader]"))
        {
            for(int i=0; i<ui->treeWidget_Widget->topLevelItemCount(); ++i)
            {
                QString sClassName =  ui->treeWidget_Widget->topLevelItem(i)->data(0,Qt::WhatsThisRole).toString();
                QString sWidgetTitle = ui->treeWidget_Widget->topLevelItem(i)->data(0,Qt::DisplayRole).toString();
                QString sParWidget = ui->treeWidget_Widget->topLevelItem(i)->data(1,Qt::DisplayRole).toString();
                QString sIconPath = ui->treeWidget_Widget->topLevelItem(i)->data(2,Qt::DisplayRole).toString();
//                bool bgenUIFile = ui->treeWidget_Widget->topLevelItem(i)->data(3,Qt::WhatsThisRole).toBool();

                QString sOut = QString("#include \"%1.h\"").arg(sClassName.toLower());
                out<<sOut<<"\r\n";

            }
            continue;
        }
        if(sLine.contains("[action]"))
        {
            for(int i=0; i<ui->treeWidget_Action->topLevelItemCount(); ++i)
            {
                QString sActionText = ui->treeWidget_Action->topLevelItem(i)->data(0,Qt::DisplayRole).toString();
                QString sNew = QString("    QAction * mp%1;").arg(sActionText);

                out<<sNew<<"\r\n";

            }
            continue;
        }
        if(sLine.contains("[widget]"))
        {
            for(int i=0; i<ui->treeWidget_Widget->topLevelItemCount(); ++i)
            {
                QString sClassName =  ui->treeWidget_Widget->topLevelItem(i)->data(0,Qt::WhatsThisRole).toString();
                QString sNew = sLine;
                sNew = sNew.replace("[widget]",sClassName);
                out<<sNew<<"\r\n";

            }
            continue;
        }
        if(sLine.contains("[actionslot]"))
        {
            for(int i=0; i<ui->treeWidget_Action->topLevelItemCount(); ++i)
            {
                QString sActionText = ui->treeWidget_Action->topLevelItem(i)->data(0,Qt::DisplayRole).toString();

                QString sNew = QString("    void slot_%1();").arg(sActionText);
                out<<sNew<<"\r\n";

            }
            continue;
        }

        QString sOut = sLine;
        sOut = sOut.replace("[name]",sClassName);
        sOut = sOut.replace("[name_lower]",sClassName.toLower());
        sOut = sOut.replace("[name_upper]",sClassName.toUpper());

        out<<sOut<<"\r\n";
    }

    proFile.close();
    tempFile.close();
}

void BundleDesignerDialog::generateBundleCpp(const QString &dir)
{
    QFile tempFile(":/ModelFile/BundleDesigner/bundlecpp.temp");
    if(!tempFile.open(QIODevice::ReadOnly))
    {
        return;
    }

    QTextStream in(&tempFile);

    QDir fileDir(dir);
    QString sClassName =  ui->lineEdit_Name->text();
    QFile cppFile(fileDir.absolutePath()+"/"+sClassName.toLower()+".cpp");
    if(!cppFile.open(QIODevice::WriteOnly))
    {
        return;
    }
    QTextStream out(&cppFile);

    while(!in.atEnd())
    {
//        QAction* pAction = new QAction(tr("[name]TestAction"),nullptr);
//        connect(pAction,SIGNAL(triggered()),this,SLOT(testActionSlot()));
//        hello->addFrameAction(tr("TestPage"),tr("TestGroup"),pAction);

        QString sLine = in.readLine();
        if(sLine.contains("[actionstart]"))
        {
            for(int i=0; i<ui->treeWidget_Action->topLevelItemCount(); ++i)
            {
                QString sClassName =  ui->lineEdit_Name->text();
                QString sActionText = ui->treeWidget_Action->topLevelItem(i)->data(0,Qt::DisplayRole).toString();
//                int style = ui->treeWidget_Action->topLevelItem(i)->data(0,Qt::DisplayRole).toInt();
                QString iconPath = ui->treeWidget_Action->topLevelItem(i)->data(3,Qt::WhatsThisRole).toString();
                QString sActionContext = ui->treeWidget_Action->topLevelItem(i)->data(2,Qt::WhatsThisRole).toString();
                QString sIconName = iconPath.section("/",-1,-1);
                QString sPageName = ui->treeWidget_Action->topLevelItem(i)->data(1,Qt::WhatsThisRole).toString();
                QString sGroupName = ui->treeWidget_Action->topLevelItem(i)->data(2,Qt::WhatsThisRole).toString();

                QString sNew = QString("        mp%1 = new QAction(QIcon(\":/res/%2\"),tr(\"%3\"),nullptr);").arg(sActionText).arg(sIconName).arg(sActionText);
                QString sConnect = QString("        connect(mp%1,SIGNAL(triggered()),this,SLOT(slot_%2()));").arg(sActionText).arg(sActionText);
                QString sCom = QString("        // please modify page and group name");
                QString sAddAction = QString("        hello->addFrameAction(QString(\"%1\"),QString(\"%2\"),mp%3);").arg(sPageName).arg(sGroupName).arg(sActionText);

                out<<sNew<<"\r\n";
                out<<sConnect<<"\r\n";
                out<<sCom<<"\r\n";
                out<<sAddAction<<"\r\n";
                out<<"\r\n";

            }
            continue;
        }
        if(sLine.contains("[actionslot]"))
        {
            for(int i=0; i<ui->treeWidget_Action->topLevelItemCount(); ++i)
            {
                QString sActionText = ui->treeWidget_Action->topLevelItem(i)->data(0,Qt::DisplayRole).toString();

                QString sOut = QString("void %1::slot_%2()").arg(sClassName).arg(sActionText);
                out<<sOut<<"\r\n";
                out<<"{"<<"\r\n";
                out<<"\r\n";
                out<<"}"<<"\r\n";
                out<<"\r\n";

            }
            continue;
        }
        if(sLine.contains("[widgetstart]"))
        {
            //        QWidget* pWidget = new QWidget();
            //        pWidget->setWindowTitle(tr("[name]TestWidget"));
            //        pWidget->setObjectName(QString("ObjectWidget[name]TestWidget"));
            //        hello->addFrameCenterWidget(pWidget);
            for(int i=0; i<ui->treeWidget_Widget->topLevelItemCount(); ++i)
            {
                QString sClassName =  ui->treeWidget_Widget->topLevelItem(i)->data(0,Qt::WhatsThisRole).toString();

                QString sNew = QString("        mp%1 = new %1();").arg(sClassName);
                QString sAdd = QString("        hello->addFrameCenterWidget(mp%1);").arg(sClassName);
                out<<sNew<<"\r\n";
                out<<sAdd<<"\r\n";
                out<<"\r\n";

            }
            continue;
        }
        if(sLine.contains("[actionstop]"))
        {
            for(int i=0; i<ui->treeWidget_Action->topLevelItemCount(); ++i)
            {
                QString sActionText = ui->treeWidget_Action->topLevelItem(i)->data(0,Qt::DisplayRole).toString();
                QString sActionPage = ui->treeWidget_Action->topLevelItem(i)->data(1,Qt::WhatsThisRole).toString();
                QString sActionGroup = ui->treeWidget_Action->topLevelItem(i)->data(2,Qt::WhatsThisRole).toString();

                QString sRmAction = QString("        hello->removeFrameAction(QString(\"%1\"),QString(\"%2\"),mp%3);").arg(sActionPage).arg(sActionGroup).arg(sActionText);
                QString sDelAction = QString("        delete mp%1;").arg(sActionText);
                out<<sRmAction<<"\r\n";
                out<<sDelAction<<"\r\n";
                out<<"\r\n";
            }
            continue;
        }
        if(sLine.contains("[widgetstop]"))
        {
            for(int i=0; i<ui->treeWidget_Widget->topLevelItemCount(); ++i)
            {
                QString sClassName =  ui->treeWidget_Widget->topLevelItem(i)->data(0,Qt::WhatsThisRole).toString();

                QString sRmWidget = QString("        hello->removeFrameCenterWidget(mp%1);").arg(sClassName);
                QString sDelWidget = QString("        delete mp%1;").arg(sClassName);
                out<<sRmWidget<<"\r\n";
                out<<sDelWidget<<"\r\n";
                out<<"\r\n";
            }
            continue;
        }

        QString sOut = sLine;
        sOut = sOut.replace("[name]",sClassName);
        sOut = sOut.replace("[name_lower]",sClassName.toLower());
        sOut = sOut.replace("[name_upper]",sClassName.toUpper());

        out<<sOut<<"\r\n";
    }

    cppFile.close();
    tempFile.close();
}

void BundleDesignerDialog::generateWidgetFile(const QString &dir,int row)
{
    if(ui->treeWidget_Widget->topLevelItemCount()-1<row)
    {
        qWarning()<<tr("toplevel count is wrong");
        return;
    }

    QFile tempFile(":/ModelFile/BundleDesigner/widget.temp");
    if(!tempFile.open(QIODevice::ReadOnly))
    {
        qWarning()<<"tempfile open failed";
        return;
    }

    QTextStream in(&tempFile);

    QDir fileDir(dir);

    QString sClassName =  ui->treeWidget_Widget->topLevelItem(row)->data(0,Qt::WhatsThisRole).toString();
    QString sWidgetTitle = ui->treeWidget_Widget->topLevelItem(row)->data(0,Qt::DisplayRole).toString();
    QString sParWidget = ui->treeWidget_Widget->topLevelItem(row)->data(1,Qt::DisplayRole).toString();
    QString sIconPath = ui->treeWidget_Widget->topLevelItem(row)->data(2,Qt::DisplayRole).toString();
    bool bgenUIFile = ui->treeWidget_Widget->topLevelItem(row)->data(3,Qt::WhatsThisRole).toBool();

    QString sIconFileName = sIconPath.section("/",-1,-1);

    qDebug()<<sClassName<<sWidgetTitle<<sParWidget<<sIconFileName;

#if 1// header
    QFile headerFile(fileDir.absolutePath()+"/"+sClassName.toLower()+".h");
    if(!headerFile.open(QIODevice::WriteOnly))
    {
        qWarning()<<"headerfile open failed";
        return;
    }
    QTextStream out(&headerFile);

    int nRunFlag = 0;// 0表示还没找到开始，1表示已经开始，还未结束，2表示已经结束，事实上，不会出现2
    while(!in.atEnd())
    {
        QString sLine = in.readLine();
        if(nRunFlag == 0)
        {
            if(sLine.contains("[headerbegin]"))
            {
                nRunFlag = 1;
            }
            continue;
        }
        else if(nRunFlag == 1)
        {
            if(sLine.contains("[headerend]"))
            {
                nRunFlag = 2;
                break;
            }
            else if(sLine.contains("[genuifile]"))
            {
                if(bgenUIFile)
                {
                    QString sOut = sLine;
                    sOut = sOut.replace("[name]",sClassName);
                    sOut = sOut.replace("[name_lower]",sClassName.toLower());
                    sOut = sOut.replace("[name_upper]",sClassName.toUpper());
                    sOut = sOut.replace("[genuifile]","");
                    out<<sOut<<"\r\n";
                }
                else
                {
                    continue;
                }
            }
            else
            {
                QString sOut = sLine;
                sOut = sOut.replace("[name]",sClassName);
                sOut = sOut.replace("[name_lower]",sClassName.toLower());
                sOut = sOut.replace("[name_upper]",sClassName.toUpper());
                if(sParWidget.contains("QTableWidget") ||
                        sParWidget.contains("QWidget") ||
                        sParWidget.contains("QListWidget") ||
                        sParWidget.contains("QMainWindow") ||
                        sParWidget.contains("QTreeWidget"))
                {
                    sOut = sOut.replace("[name]",sClassName);
                    sOut = sOut.replace("[name_lower]",sClassName.toLower());
                    sOut = sOut.replace("[name_upper]",sClassName.toUpper());
                    sOut = sOut.replace("[parentclass]",QString("%1").arg(sParWidget));
                    sOut = sOut.replace("[parentclassheader]",QString("<%1>").arg(sParWidget));

                }
                else
                {
                    sOut = sOut.replace("[name]",sClassName);
                    sOut = sOut.replace("[name_lower]",sClassName.toLower());
                    sOut = sOut.replace("[name_upper]",sClassName.toUpper());
                    sOut = sOut.replace("[parentclass]",QString("%1").arg(sParWidget));
                    sOut = sOut.replace("[parentclassheader]",QString("\"%1.h\"").arg(sParWidget.toLower()));

                }

                out<<sOut<<"\r\n";
            }
        }
        else
        {
            break;
        }

    }

    headerFile.close();
#endif

#if 2// cpp file
    QFile cppFile(fileDir.absolutePath()+"/"+sClassName.toLower()+".cpp");
    if(!cppFile.open(QIODevice::WriteOnly))
    {
        qWarning()<<"cppfile open failed";

        return;
    }
    QTextStream cppout(&cppFile);

    nRunFlag = 0;// 0表示还没找到开始，1表示已经开始，还未结束，2表示已经结束，事实上，不会出现2
    in.seek(0);
    while(!in.atEnd())
    {
        QString sLine = in.readLine();
        if(nRunFlag == 0)
        {
            if(sLine.contains("[cppbegin]"))
            {
                nRunFlag = 1;
            }
            continue;
        }
        else if(nRunFlag == 1)
        {
            if(sLine.contains("[cppend]"))
            {
                nRunFlag = 2;
                break;
            }
            else if(sLine.contains("[genuifile]"))
            {
                if(bgenUIFile)
                {
                    QString sOut = sLine;
                    sOut = sOut.replace("[name]",sClassName);
                    sOut = sOut.replace("[name_lower]",sClassName.toLower());
                    sOut = sOut.replace("[name_upper]",sClassName.toUpper());
                    sOut = sOut.replace("[genuifile]","");
                    cppout<<sOut<<"\r\n";
                }
                else
                {
                    continue;
                }
            }
            // for hplot widget initial
            else if(sLine.contains("[hplotinitialbegin]"))
            {
                sLine = in.readLine();
                while(!sLine.contains("[hplotinitialend]"))
                {
                    if(sParWidget == "HPlot")
                        cppout<<sLine<<"\r\n";
                    sLine = in.readLine();
                }
                continue;
            }
            else
            {
                QString sOut = sLine;
                sOut = sOut.replace("[name]",sClassName);
                sOut = sOut.replace("[name_lower]",sClassName.toLower());
                sOut = sOut.replace("[name_upper]",sClassName.toUpper());
                sOut = sOut.replace("[parentclass]",sParWidget);
                sOut = sOut.replace("[windowtitle]",sWidgetTitle);
                sOut = sOut.replace("[iconpath]",QString(":/res/%1").arg(sIconFileName));

                cppout<<sOut<<"\r\n";
            }
        }
        else
        {
            break;
        }

    }

    cppFile.close();
#endif

#if 3
    if(bgenUIFile)
    {
        QFile uiFile(fileDir.absolutePath()+"/"+sClassName.toLower()+".ui");
        if(!uiFile.open(QIODevice::WriteOnly))
        {
            qWarning()<<"uiFile open failed";

            return;
        }
        QTextStream cppout(&uiFile);

        nRunFlag = 0;// 0表示还没找到开始，1表示已经开始，还未结束，2表示已经结束，事实上，不会出现2
        in.seek(0);
        if(sParWidget == "QWidget")
        {
            while(!in.atEnd())
            {
                QString sLine = in.readLine();
                if(nRunFlag == 0)
                {
                    if(sLine.contains("[widgetuibegin]"))
                    {
                        nRunFlag = 1;
                    }
                    continue;
                }
                else if(nRunFlag == 1)
                {
                    if(sLine.contains("[widgetuiend]"))
                    {
                        nRunFlag = 2;
                        break;
                    }
                    else
                    {
                        QString sOut = sLine;
                        sOut = sOut.replace("[name]",sClassName);
                        sOut = sOut.replace("[windowtitle]",sWidgetTitle);

                        cppout<<sOut<<"\r\n";
                    }
                }
                else
                {
                    break;
                }

            }
        }
        else if(sParWidget == "QMainWindow")
        {
            while(!in.atEnd())
            {
                QString sLine = in.readLine();
                if(nRunFlag == 0)
                {
                    if(sLine.contains("[mainwindowuibegin]"))
                    {
                        nRunFlag = 1;
                    }
                    continue;
                }
                else if(nRunFlag == 1)
                {
                    if(sLine.contains("[mainwindowuiend]"))
                    {
                        nRunFlag = 2;
                        break;
                    }
                    else
                    {
                        QString sOut = sLine;
                        sOut = sOut.replace("[name]",sClassName);
                        sOut = sOut.replace("[windowtitle]",sWidgetTitle);

                        cppout<<sOut<<"\r\n";
                    }
                }
                else
                {
                    break;
                }

            }
        }
        uiFile.close();
    }

#endif

    tempFile.close();
}

void BundleDesignerDialog::on_pushButton_AddAction_clicked()
{
    ActionDialog dlg(this);
    if(dlg.exec() == QDialog::Accepted)
    {
        QTreeWidgetItem* pTopItem = new QTreeWidgetItem();
        dlg.updateTreeItem(pTopItem);
#if 0
        QString sActionText = dlg.actionText();
        QString sActionContext = dlg.actionContext();
//        bool bCheckable = dlg.actionCheckable();
//        bool bChecked = dlg.actionCheckedDefault();
        QString iconPath = dlg.actionIcon();
        int style = dlg.actionStyle();
        QString sActionPage = dlg.actionPage();
        QString sActionGroup = dlg.actionGroup();

        pTopItem->setData(0,Qt::DisplayRole,sActionText);
        pTopItem->setData(0,Qt::WhatsThisRole,style);

        pTopItem->setData(1,Qt::DisplayRole,QString("%1").arg(sActionPage));
        pTopItem->setData(1,Qt::WhatsThisRole,iconPath);
        pTopItem->setData(2,Qt::DisplayRole,QString("%1").arg(sActionGroup));
        pTopItem->setData(2,Qt::DisplayRole,QString("%1-%2").arg(sActionPage).arg(sActionGroup));
        pTopItem->setData(3,Qt::DisplayRole,sActionContext);
        pTopItem->setData(3,Qt::WhatsThisRole,iconPath);
//        pTopItem->setData(3,Qt::WhatsThisRole,bCheckable);
//        pTopItem->setData(4,Qt::WhatsThisRole,bChecked);

        pTopItem->setIcon(0,QIcon(iconPath));
#endif
        ui->treeWidget_Action->addTopLevelItem(pTopItem);

    }
    else
    {
        return;
    }
}

void BundleDesignerDialog::on_pushButton_RemoveAction_clicked()
{
    QTreeWidgetItem* pCurItem = ui->treeWidget_Action->currentItem();
    if(nullptr == pCurItem)
    {
        return;
    }

    ui->treeWidget_Action->takeTopLevelItem(ui->treeWidget_Action->indexOfTopLevelItem(pCurItem));
}

void BundleDesignerDialog::on_pushButton_AddWidget_clicked()
{
    WidgetDialog dlg(this);
    if(dlg.exec() == QDialog::Accepted)
    {
        QString sTitle = dlg.widgetTitle();
        QString sParWidget = dlg.parentWidget();
        QString iconPath = dlg.widgetIcon();
        QString sClassName = dlg.widgetClassName();
        bool bgenUI = dlg.generateUIFile();

        QTreeWidgetItem* pTopItem = new QTreeWidgetItem();
        pTopItem->setData(0,Qt::DisplayRole,sTitle);
        pTopItem->setData(0,Qt::WhatsThisRole,sClassName);
        pTopItem->setData(1,Qt::DisplayRole,sParWidget);
        pTopItem->setData(2,Qt::DisplayRole,iconPath);
        pTopItem->setData(3,Qt::WhatsThisRole,bgenUI);
        pTopItem->setIcon(0,QIcon(iconPath));

        ui->treeWidget_Widget->addTopLevelItem(pTopItem);
    }
    else
    {
        return;
    }
}

void BundleDesignerDialog::on_pushButton_RemoveWidget_clicked()
{
    QTreeWidgetItem* pCurItem = ui->treeWidget_Widget->currentItem();
    if(nullptr == pCurItem)
    {
        return;
    }

    ui->treeWidget_Widget->takeTopLevelItem(ui->treeWidget_Widget->indexOfTopLevelItem(pCurItem));
}
